/**
 * 定义分页全局变量
 */
var page_ = new page({
	search : search
});
/**
 * 定义容器全局变量
 */
var con = new container({
	datagrid : '#datagrid',
	datagridtoolbar : '#datagrid-toolbar',
	singleSelect : true
});

$(document).ready(function() {
	/**
	 * 查询按钮设置
	 */
	$("#search").linkbutton({
		onClick : function() {
			search(page_);
		}
	});	
	/**
	 * 查看信息设置
	 */
	$("#review").linkbutton({
		onClick : function() {
			detail();
		}
	});
	/**
	 * 回信按钮设置
	 */
	$("#back_message").linkbutton({
		onClick : function() {
			backMessage();
		}
	});
	/**
	 * 删除按钮设置
	 */
	$("#del_message").linkbutton({
		onClick : function() {
			delMessage();
		}
	});
	init_user();
	
});
/**
 * 初始化发件人(根据organId)
 */
function init_user(){
	var condition = $("#listform").serializeJson();
	condition.oid = $("#listform #organId").val();
	$.ajax({
		type : "GET",
		url : ctx + "/user/query",
		dataType : "json",
		data : condition,
		success : function(data) {			
			if(!showMsg(data)){
				return;
			}
			$('#senduser').combobox({
				valueField : 'id',
				textField : 'username',
				data : data.content.content,
			});
			search(page_);
		}
	});
}
/**
 * 获取查询条件 
 * @returns
 */
function getCondition() {
	var condition = $("#listform").serializeJson();
	condition.userName = $("#senduser").combobox('getText');
	condition.addresseeName = $("#listform #addressee").combobox('getText');
	if($.trim(condition.addresseeName)==''){
		condition.addresseeName = $("#listform #userName").val();
	}
	condition.userId = $("#listform #userId").val();	
	return condition;
}

/**
 * 查询
 */
function search(pg) {
	var url = ctx + "/receiveMessage/query";
	page_.pageSize = pg.pageSize;
	page_.pageNumber = pg.pageNumber;
	query(url, getCondition(), con, page_);
}
/**
 * 邮件查看
 */
function detail(){
	readMessage();
	var editRow = getSelectedRow("#datagrid");
	if (!editRow) {
		return;
	}
	$('#dialog').dialog({
		title : '邮件详情',
		width : 650,
		height : 510,
		left : 100,
		top : 20,
		href : 'message/detail.jsp',
		closed : false,
		cache : false,
		modal : true,
		onLoad : function() {
			$("#titleShow").textbox("setValue",editRow.title);
			$('#titleShow').textbox({ editable : false});
			$("#contentShow").textbox("setValue",editRow.content);
			$('#contentShow').textbox({ editable : false});
			$("#adduserShow").textbox("setValue",editRow.userName);
			$('#adduserShow').textbox({ editable : false});
			$("#addresseeNameShow").textbox("setValue",editRow.addresseeName);
			$('#addresseeNameShow').textbox({ editable : false});
			$("#addresseeTypeNameShow").textbox("setValue",editRow.addresseeTypeName);
			$('#addresseeTypeNameShow').textbox({ editable : false});
			init_AddresseeType(editRow);
		}
	});
}

/**
 * 回信
 */
function backMessage() {
	var editRow = getSelectedRow("#datagrid");
	if (!editRow) {
		return;
	}
	$('#dialog').dialog({
		title : '回信',
		width : 820,
		height : 470,
		left : 100,
		top : 20,
		href : 'message/add.jsp',
		closed : false,
		cache : false,
		modal : true,
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
		onLoad : function() {
			init_AddresseeType(editRow);
			$('#addAddressee').linkbutton('disable');
			$('#delAddressee').linkbutton('disable');
			$("#titleRe").textbox("setValue",editRow.title);
			$('#titleRe').textbox({ editable : false});
			$("#contentRe").textbox("setValue",editRow.content);
			$('#contentRe').textbox({ editable : false});
		},
		buttons : [ {
			text : '保存',
			iconCls : 'icon-ok',
			handler : function() {
				update(editRow);
				$('#dialog').dialog('close');
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				$('#dialog').dialog('close');
			}
		} ]
	});
};

/**
 * 删除信息
 */
function delMessage() {
	var row = getSelectedRow("#datagrid");
	if (!row) {
		return;
	}
	$.messager.confirm('提示', '确定删除?', function(r) {
		if (r) {
			$.ajax({
				type : "POST",
				url : ctx + "/receiveMessage/delete",
				dataType : "json",
				data : {
					id : row.receiveMessageId
				},
				success : function(data) {
					if(!showMsg(data,"删除成功")){
						return;
					}
					search(page_);
				},
			});
		}
	});
}

/**
 * 回信
 */
function update(editRow) {
	var remind = $("input[name='remind']:checked").val();
	var data = {
			remind : remind,
			receiveMessageId : editRow.receiveMessageId,
			UserName : $("#adduser").combobox('getText'),
			reContent : $("#reContent").textbox('getText'),
	};
	$.ajax({
		type : "POST",
		url : ctx + "/receiveMessage/backMessage",
		dataType : "json",
		data : data,
		success : function(data) {
			if(!showMsg(data,"发送成功")){
				return;
			}
			search(page_);
		},
	});
}

/**
 * 已读信息
 */
function readMessage(){
	var userId = $("#userId").val();
	var row = getSelectedRow("#datagrid");
	$.ajax({
		type : "POST",
		url : ctx + "/readMessage/insert",
		dataType : "json",
		data : {
			messageId : row.receiveMessageId,userId : userId	
		},
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
		},
	});
}